package com.jzo2o.customer.controller.consumer;


import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 我的地址簿相关接口
 * @author Administrator
 */
@RestController("consumerAddressBookController")
@RequestMapping("/consumer/address-book")
@Api(tags = "用户端 - 我的地址簿相关接口")
@Slf4j
public class AddressBookController {

    @Resource
    IAddressBookService addressBookService;

    @PostMapping
    @ApiOperation("新增地址簿")
    public void addAddress(@RequestBody AddressBookUpsertReqDTO dto){
        log.info("新增地址簿接收的参数:{}",dto);
        addressBookService.addAddress(dto);
    }

    @GetMapping("/page")
    @ApiOperation("地址薄分页查询")
    public PageResult<AddressBook> getAddressByPage(AddressBookPageQueryReqDTO dto){
        return addressBookService.getAddressByPage(dto);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址簿")
    public AddressBook getAddressById(@PathVariable Long id){
        log.info("根据id查询地址簿接收的参数:{}",id);
        return addressBookService.getById(id);
    }

    @PutMapping("/{id}")
    @ApiOperation("修改地址簿")
    public void updateAddressById(@RequestBody AddressBookUpsertReqDTO dto,@PathVariable Long id){
        log.info("修改地址簿接收的参数:{},{}",dto,id);
        addressBookService.updateAddressById(dto,id);
    }

    @DeleteMapping("/batch")
    @ApiOperation("地址簿批量删除")
    public void batchDeleteAddress(@RequestBody List<Long> ids){
        log.info("地址簿批量删除参数:{}",ids);
        addressBookService.batchDeleteAddress(ids);
    }

    @PutMapping("/default")
    @ApiOperation("设置/取消默认地址")
    public void updateDefault(Integer flag,Long id){
        log.info("默认状态:{},id:{}",flag,id);
        addressBookService.updateDefault(flag,id);
    }

    @GetMapping("/defaultAddress")
    @ApiOperation("获取默认地址")
    private AddressBook defaultAddress(){
        return addressBookService.selectDefaultAddress();
    }

}
